/* eslint-disable func-names */
import { painManagementRecord } from '@UTILS/track/trackUtils'
import { isInternalAccount } from './internalAccount'
// function trackPageView(page, prePage) {
//   window._czc.push(['_trackPageview', page]);
// }

export async function trackEvent(category, action, sceneData, value, nodeid) {
  if (await isInternalAccount()) {
    return
  }
  if (sceneData) {
    painManagementRecord(category, action, sceneData)
  }
  window._czc.push(['_trackEvent', category, action, '', value || 1, nodeid || ''])
}

const setUserId = userId => {
  console.log('SetUserId', userId)
}

const init = appId => {
  // 友盟统计
  // 1279382521 开发， 1279382515 测试
  const [cdn, umengAnalyId] = appId.split('_')
  window._czc = window._czc || []
  window._czc.push(['_setAccount', umengAnalyId])
  window._czc.push(['_setAutoPageview', false]);
  (function () {
    const hm = document.createElement('script')
    hm.async = true
    hm.charset = 'utf-8'
    hm.src = `https://${cdn}.cnzz.com/z_stat.php?id=${umengAnalyId}&web_id=${umengAnalyId}`
    const s = document.getElementsByTagName('script')[0]
    s.parentNode.insertBefore(hm, s)
  }())

  window._czc.push(['_trackEvent', 'newApp', 'initApp'])
  // setCustom()
  // history && history.listen(location => {
  //   // console.log('tracking page view: ', location.pathname);
  //   if(window['_czc']) { // eslint-disable-line
  //     _czc.push(['_trackPageview', location.pathname]); // eslint-disable-line
  //   } else {
  //     console.error('没有初始化统计代码');
  //   }
  // });
}

export default { trackEvent, setUserId, init }
